Syntax problems (as usual)

Hi,
stuck with the syntax again.

I want to stop execution, if the state of a AC device is 'off'. The condition let the workflow running on when the result is true, so I have to check for 'is not off'

         .......
- condition: template
            value_template: "{{ not (states(myAcDevice) == 'off') }}"
          - action: climate.set_temperature
                 .....

I checked the current state of my device with development tools and it shows 'fan_only' but execution stops.

Whats going on here?. The content of 'myAcDevice' is ok, because I can see that everything in my blueprint works fine.

Only the test fails.

Thanks for heelping out.

It might be helpful to share more before and after. (or the whole yaml) to get a better answer.

2 Likes

What does the debug Trace show?

try
{{ not is_state('sensor.myAcDevice', 'off') }}

assuming myAcDevice is a variable containing a properly formatted entity_id:

          - if: "{{ states(myAcDevice) == 'off' }}"
            then:
            - stop: "Stop running the rest of the sequence"

or

          - condition: template
            value_template: "{{ not is_state(myAcDevice, 'off') }}"

or

          - condition: template
            value_template: "{{ states(myAcDevice) != 'off' }}"

Hi petro,
thanks, but not one of this lines is working.

There is no messagee about any error, just had result 'false' and so do the wrong thing.

BUT: Asking me, that in the variable it needs an 'climate' device triggers more investigation. Whenever I used the variable in my blueprint to carry out some action, the AC box do it. For me: "Content of this variable correct". But the Studio server shows some message which I ignored (as somebody told me, that this messages were not reliable):slight_smile:

I immediately checked the configuration.yaml and there is the 'climate-line'.


logger:
  default: info
  logs:
    homeassistant.components.python_script: debug
    homeassistant.components.automation: info
    homeassistant.components.mqtt.discovery: info

climate:
  #- platform: local_daikin
  #  ip_address:
  #    - 192.168.0.170

  ######   Thermostate (PID) KG   ######
  #   Thermostat für Turnsaal
  - platform: smart_thermostat
    name: KG Turnsaal Thermostat WP
    unique_id: kg_turnsaal_thermostat_wp
    heater: input_boolean.kg_turnsaal_wp
    target_sensor: sensor.kg_turnsaal_tempmeter_temp
    outdoor_sensor: sensor.out_tempmeter_temp
    min_temp: 15
    max_temp: 28
    ac_mode: False
    target_temp: 23
    keep_alive:
      seconds: 60
    away_temp: 14
    kp: 5
    ki: 0.01
    kd: 500
    ke: 0.5
    pwm: 00:15:00

I use some PID thermostates for the heating (not AC boxes) system since about 2 years without any problems. And all my automations for AC boxes without blueprints working fine. And some functions of the AC boxes in blueprints working fine (Switching On/Off, setting target temperature ...) only checking the state doesn't work out.

Here the whole yaml for SirGoodEnough, but it is much more than the part causing the problems:

blueprint:
  name: Air Condition Controller V2
  description: Controls an air condition box in a room, stops using the remote control and takes care of open windows/doors and black out condition.
  domain: automation
  author: DI Gerhard Kreuzer
  
  input:
    main_switch:
      name: Main Switch
      selector:
        entity:
          filter:
            domain: input_boolean
    room_switch:
      name: Room Switch
      selector:
        entity:
          filter:
            domain: input_boolean
    remote_switch:
      name: Remote Power Switch
      selector:
        entity:
          filter:
            domain: switch
    blackout_signal:
      name: Signals a blackout condition
      selector:
        entity:
          filter:
            domain: input_boolean
    room_state:
      name: Room State
      description: Signals, that the room is ready for cooling/heating (all windows or doors were closed).
      selector:
        entity:
          filter:
            domain: binary_sensor
    room_closed_delay_seconds:
      name: Delay start of AC after romm is closed
      description: Delay action after room is closed for x seconds
      default: 30
      selector:
        number:
          min: 10
          max: 300
          step: 5
          unit_of_measurement: seconds
    room_opened_delay_seconds:
      name: Delay stop of AC after room is  opened
      description: Delay action after room is opened for x seconds
      default: 30
      selector:
        number:
          min: 10
          max: 300
          step: 5
          unit_of_measurement: seconds
    override_delay_seconds:
      name: Delay the override action after usage of remote control
      description: Delay overruling of user setting (with remote control) for x seconds
      default: 30
      selector:
        number:
          min: 10
          max: 300
          step: 5
          unit_of_measurement: seconds
    target_temperature:
      name: Target Temperature Value
      selector:
        entity:
          filter:
            domain: 
            - input_number
    room_temperature:
      name: Room Temperature Sensor
      selector:
        entity:
          filter:
            domain: sensor
            device_class: temperature
    ac_mode:
      name: Mode of the AC (Cool, Heat, Dry, ...)
      selector:
        entity:
          filter:
            domain: 
            - input_select
    preselection:
      name: Preselected Mode of Thermostat (Eco, Boost, ...)
      selector:
        entity:
          filter:
            domain: 
            - input_select
    ac_device:
      name: AC Device to be controlled
      selector:
        entity:
          multiple: true
          filter:
            domain: 
            - climate

mode: single #queued

variables:
  myBlackoutInput:      !input blackout_signal
  myBlackoutSignal:     '{{ states(myBlackoutInput) == ''on''}}'
  myMainSwitchInput:    !input main_switch
  myMainSwitch:         '{{ states(myMainSwitchInput) == ''on''}}'
  myRoomSwitchInput:    !input room_switch
  myRoomSwitch:         '{{ states(myRoomSwitchInput) == ''on''}}'
  myRemoteSwitch:       !input remote_switch
  myIsWindowOpenInput:  !input room_state
  myIsWindowOpen:       '{{ states(myIsWindowOpenInput) == ''on''}}'
  myTargetTemp:         !input target_temperature
  myRoomTemperature:    !input room_temperature
  myAcMode:             !input ac_mode
  myPreselection:       !input preselection
  myRoomIsOpenDelay:    !input room_opened_delay_seconds
  myRoomIsClosedDelay:  !input room_closed_delay_seconds
  myOverrideDelay:      !input override_delay_seconds
  myAcDevice:           !input ac_device
  myRCIsOn:             '{{ saver.entity(myRemoteSwitch) == ''on''}}'
  myEnabled:            '{{ not myBlackoutSignal and myMainSwitch and myRoomSwitch and not myIsWindowOpen }}'

triggers:
  - trigger: state
    entity_id: !input blackout_signal
    from:
      - "off"
    to:
      - "on"
    id: Blackout hat begonnen

  - trigger: state
    entity_id: !input blackout_signal
    from:
      - "on"
    to:
      - "off"
    for:
      hours: 0
      minutes: 10
      seconds: 0
    id: Blackout endet

  - trigger: state
    entity_id: !input main_switch
    from:
      - "off"
    to:
      - "on"
    id: Main Switched On

  - trigger: state
    entity_id: !input main_switch
    from:
      - "on"
    to:
      - "off"
    id: Main Switched Off

  - trigger: state
    entity_id: !input room_switch
    from:
      - "off"
    to:
      - "on"
    id: Room Switched On

  - trigger: state
    entity_id: !input room_switch
    from:
      - "on"
    to:
      - "off"
    id: Room Switched Off

  - trigger: state
    entity_id: !input remote_switch
    from:
      - "off"
    to:
      - "on"
    id: Remote Switched on
    #enabled: false
    for:
      hours: 0
      minutes: 0
      seconds: !input override_delay_seconds

  - trigger: state
    entity_id: !input remote_switch
    from:
      - "on"
    to:
      - "off"
    id: Remote Switched off

  - trigger: state
    entity_id: !input ac_device
    attribute: hvac_action
    id: Set AC Mode from off to something
    # enabled: false
    for:
      hours: 0
      minutes: 0
      seconds: !input override_delay_seconds
    from:
      - "off"

  - trigger: state
    entity_id: !input ac_device
    attribute: hvac_action
    id: AC Mode set to Off
    # enabled: false
    for:
      hours: 0
      minutes: 0
      seconds: !input override_delay_seconds
    to:
      - "off"

  - trigger: state
    entity_id: !input ac_mode
    id: AC Mode zentral geƤndert

  - trigger: state
    entity_id: !input target_temperature
    id: Zentral vorgegebene Solltemperatur wurde geƤndert

  - trigger: state
    entity_id: !input ac_device
    attribute: temperature
    id: Soll-Temperatur wurde remote geƤndert
    for:
      hours: 0
      minutes: 0
      seconds: !input override_delay_seconds

  - trigger: state
    entity_id: !input preselection
    id: AC Level geƤndert

  - trigger: state
    entity_id: !input ac_device
    attribute: preset_mode
    for:
      hours: 0
      minutes: 0
      seconds: !input override_delay_seconds
    id: Voreinstellung remote geƤndert

  - trigger: state
    entity_id: !input room_state
    from: "off"
    to: "on"
    id: Window or Door opened
    for:
      hours: 0
      minutes: 5
      seconds: 0

  - trigger: state
    entity_id: !input room_state
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: !input room_closed_delay_seconds
    id: Window or Door closed

  - trigger: time_pattern
    minutes: /1
    seconds: "15"
    id: TickTack

conditions: []

actions:
  - choose:
      # Save current state and switch AC off, because AC is not usable anymore
      - conditions:
          - condition: trigger
            id:
              - Blackout hat begonnen
              - Main Switched Off
              - Room Switched Off
              - Window or Door opened
        sequence:
          - action: saver.save_state
            data:
              entity_id: !input ac_device
          - action: climate.turn_off
            metadata: {}
            target:
              entity_id: !input ac_device
            data: {}

      # If AC is usable again restore last state
      - conditions:
          - condition: trigger
            id:
              - Blackout endet
              - Main Switched On
              - Room Switched On
              - Window or Door closed
        sequence:
          - condition: template
            value_template: '{{ myEnabled == true }}'
          - action: saver.restore_state
            data:
              entity_id: !input ac_device
              delete_after_run: false
          #- condition: template
          #  value_template: '{{ not (states(myAcDevice) == ''off'') }}'
          #- action: climate.turn_on
          #  metadata: {}
          #  target:
          #    entity_id: !input ac_device
          #  data: {}

      # Somebody used the remote control to switch off his AC
      - conditions:
          - condition: trigger
            id:
              - Remote Switched off
              - AC Mode set to Off
        sequence:
          # Box is off now, save this information
          #- condition: state
          #  state:
          #    - "on"
          #  entity_id: !input remote_switch
          - action: saver.save_state
            data:
              entity_id: !input ac_device

      # Somebody tried to switch on his AC using the remote control
      - conditions:
          - condition: trigger
            id:
              - Remote Switched on
              - Set AC Mode from off to something
        sequence:
          # Box is off now, save this information
          - action: saver.save_state
            data:
              entity_id: !input ac_device
          - condition: template
            value_template: '{{ myEnabled == false }}'
          - action: climate.turn_off
            metadata: {}
            target:
              entity_id: !input ac_device
            data: {}

      - conditions:
          - condition: trigger
            id:
              - AC Mode zentral geƤndert
              - Zentral vorgegebene Solltemperatur wurde geƤndert
              - Soll-Temperatur wurde remote geƤndert
              - AC Level geƤndert
              - Voreinstellung remote geƤndert
              - TickTack
        sequence:
          - condition: template
            value_template: "{{ myEnabled == true }}"
          - condition: template
            value_template: "{{ not is_state(myAcDevice, 'off') }}"
          - action: climate.set_temperature
            metadata: {}
            data:
              temperature: >-
                {{ states( myTargetTemp ) }}
            target:
              entity_id: !input ac_device
          - action: climate.set_hvac_mode
            metadata: {}
            data:
              hvac_mode: "{{ states( myAcMode ) }}"
            target:
              entity_id: !input ac_device
          - action: climate.set_preset_mode
            metadata: {}
            data:
              preset_mode: >-
                {{ states( myPreselection )|lower }}
            target:
              entity_id: !input ac_device 

Maybe one can show me (newbee to blueprints) some better solution for some lines of code. And keep in mind, I do lot of tests to overcome this problem.

Thanks for helping out.

image

Shouldn't - platform: smart_thermostat be - platform: generic_thermostat unless another integration is in play?

I assume you are using a custom intergration

climate:
  #- platform: local_daikin

I use the PID thermostate integration and the guy creating it told us in his documentation to setup the thermostats in the configuration.yaml. The result is fine, the thermosates working for about 2 years without problems.

You can find this stuff here: ScratMan/HASmartThermostat: Smart Thermostat with PID controller for HomeAssistant

And here is the AC box as Home Assistant development tools see it:

There is a valid state. No error no problems reported.

It's an aged custom integration with quite a few issues so there may be issues attributed to HA updates.

I'll take a look at the source code.

Ok, is there better PID thermostates around? Havn't found other solution than that.

And: Why is the AC box automation ok and the blueprint made out of this automation not? Did a blueprint use a total different code base doing the automation tasks?

Sorry, still a newbee.

Based on your shared image, your PID thermostat is fine. It's a coding issue

Means what? Something I do wrong? Or something Scratman do wrong?

But thanks, I start reading the 'Climate' documentation of HA because this is an internal integration and I found 'Conditions'. There is a way to test the state of the AC box without the usage of a template. And without template there is no need to use a variable. I can use '!input ....' instead.

And: Drum rolls ..... Now it works.

- condition: climate.is_on
            target:
              entity_id: !input ac_device

I only do one quick test and checked the traces and it seems to work fine.

Now my (newbee) question:
Whats wrong with this line?

myAcDevice:           !input ac_device

Using the variable within a template fails and using the input directly works?

Sorry, cant't understand it (yet).

Where are you applying this line?

variables - section ...

See above code. I post the whole blueprint as SirGoodEnough wanted ..

Why I asked...

Ok.
But whats the reason for the problems with the templates. I am lucky here because there is a work around.

To use templates in blueprints I have to use this variables, or is this wrong? I looked over a lot of blueprints and tried to get out the syntax for constructs I needed, hoping this blueprints were working ones ....

We don't know without seeing those variables and their contents. The templates I provided will work 100% of the time assuming the entity_id is correct. If it's not working then:

  1. You have the wrong entity_id
  2. Your variable is not properly creating the correct entity_id
  3. The way you're testing the actions is not correct.

We don't know because you haven't provided all the information.


Edit, Nevermind. I see you posted the full automation above. Your problem is the variable and potentially the way you're testing it.

Your selector is marked as an entity selector with multiple: true

This means you'll be getting a list or a single entity back and you need to account for that.


  _myAcDevice:          !input ac_device
  myAcDevice: "{{ [_myAcDevice] if _myAcDevice is string else _myAcDevice }}"

Then your condition and actions:

          - variables:
              onAcDevices: >
                {% set off_devices = myAcDevice | select('is_state', 'off') | list %}
                {{ myAcDevice | reject('in', off_devices) | list }}
          - condition: template
            value_template: "{{ onAcDevices | count > 0 }}"
          - action: climate.set_temperature
            metadata: {}
            data:
              temperature: >-
                {{ states( myTargetTemp ) }}
            target:
              entity_id: "{{ onAcDevices }}"
          - action: climate.set_hvac_mode
            metadata: {}
            data:
              hvac_mode: "{{ states( myAcMode ) }}"
            target:
              entity_id: !input ac_device
          - action: climate.set_preset_mode
            metadata: {}
            data:
              preset_mode: >-
                {{ states( myPreselection )|lower }}
            target:
              entity_id: "{{ onAcDevices }}"

And the only way to test this, is to fully run the automation from a trigger. You cannot use "test actions" button on the automation.

Agrrrr. My fault. I tapped into this pitfall before and forget it! This isn't really nice, that I have to create a list with one element for myself ...

Thanks for pointing this out.

I also tapped into the pitfall using 'Test Actions', but this time I do it correct. For this reason I have my 'TickTack' now, so I always get a trigger .....