original_docstring: Listen for state changes and calls action.
docstring: Listen for state changes and calls action.
docstring_tokens: Listen for state changes and calls action .
code: def template_listener(entity_id, from_s, to_s):
          """Listen for state changes and calls action."""
          nonlocal unsub_track_same
  
          @callback
          def call_action():
              """Call action with right context."""
              hass.async_run_job(
                  action(
                      {
                          "trigger": {
                              "platform": "template",
                              "entity_id": entity_id,
                              "from_state": from_s,
                              "to_state": to_s,
                              "for": time_delta if not time_delta else period,
                          }
                      },
                      context=(to_s.context if to_s else None),
                  )
              )
  
          if not time_delta:
              call_action()
              return
  
          variables = {
              "trigger": {
                  "platform": platform_type,
                  "entity_id": entity_id,
                  "from_state": from_s,
                  "to_state": to_s,
              }
          }
  
          try:
              period = cv.positive_time_period(
                  template.render_complex(time_delta, variables)
              )
          except (exceptions.TemplateError, vol.Invalid) as ex:
              _LOGGER.error(
                  "Error rendering '%s' for template: %s", automation_info["name"], ex
              )
              return
  
          unsub_track_same = async_track_same_state(
              hass,
              period,
              call_action,
              lambda _, _2, _3: condition.async_template(hass, value_template),
              value_template.extract_entities(),
          )
code_tokens: def template_listener ( entity_id , from_s , to_s ) : nonlocal unsub_track_same @ callback def call_action ( ) : """Call action with right context.""" hass . async_run_job ( action ( { "trigger" : { "platform" : "template" , "entity_id" : entity_id , "from_state" : from_s , "to_state" : to_s , "for" : time_delta if not time_delta else period , } } , context = ( to_s . context if to_s else None ) , ) ) if not time_delta : call_action ( ) return variables = { "trigger" : { "platform" : platform_type , "entity_id" : entity_id , "from_state" : from_s , "to_state" : to_s , } } try : period = cv . positive_time_period ( template . render_complex ( time_delta , variables ) ) except ( exceptions . TemplateError , vol . Invalid ) as ex : _LOGGER . error ( "Error rendering '%s' for template: %s" , automation_info [ "name" ] , ex ) return unsub_track_same = async_track_same_state ( hass , period , call_action , lambda _ , _2 , _3 : condition . async_template ( hass , value_template ) , value_template . extract_entities ( ) , )
short_docstring: Listen for state changes and calls action.
short_docstring_tokens: Listen for state changes and calls action .
comment: """Listen for state changes and calls action."""$SEP$"""Call action with right context."""