Solved: From binary to output (Noob)

Hi.
I have 4 relays which should be activated as a simple D/A converter. I have managed to build a template to calculate the wanted output:

sensor:
  - name: "Simulator set"
    unique_id: "simulator.setting"
    state: >
      {{ (states('sensor.sal_middel')|int - 13) }}

Based on this result I can calculate the output I want like this

{{ states("sensor.simulator_vaerdi_2") | int | bitwise_and(1) }}
{{ states("sensor.simulator_vaerdi_2") | int | bitwise_and(2) }}
{{ states("sensor.simulator_vaerdi_2") | int | bitwise_and(4) }}
{{ states("sensor.simulator_vaerdi_2") | int | bitwise_and(8) }}

Now I would like to replace the “turn_off” action below using the true/false result from each calculation in the below automation:

alias: Simuler temperatur
description: Simulates pt1000 value for the actual temperature
trigger:
  - platform: state
    entity_id:
      - sensor.simulator_vaerdi_2
condition: []
action:
  - type: turn_off
    # +1 degree relay
    device_id: cd5d367f0bbae95e6c7ec91da439ce35
    entity_id: 855c33ae0a49cdb1c6dfd1e4667179f4
    domain: light
  - type: turn_off
    # +2 degree relay
    device_id: cd5d367f0bbae95e6c7ec91da439ce35
    entity_id: 6679e11def8d7b93815029f919df1d8d
    domain: light
  - type: turn_off
    # +4 degree relay
    device_id: cd5d367f0bbae95e6c7ec91da439ce35
    entity_id: 6a4fc0bc6a02877797e44ea893f61e1f
    domain: light
  - type: turn_off
    # +8 degree relay
    device_id: cd5d367f0bbae95e6c7ec91da439ce35
    entity_id: cbbd0d2ee09146c56ee528a5bff59fa0
    domain: light
mode: restart

But here I am a bit lost, on how to create the last connection…

You need to switch to Service call actions instead of Device actions, something like:

action:
  - service: light.turn_{{ 'on' if states('sensor.simulator_vaerdi_2') | int | bitwise_and(1) else 'off' }}
    # +1 degree relay
    target:
      entity_id: 855c33ae0a49cdb1c6dfd1e4667179f4
....

Normally, you would use the normal entity ID like light.relay_1_example, but the unique alphanumeric one used by a Device action will work too.

1 Like

Because templating with device_id’s is not supported, I believe you need to use entity_id’s to make this work. Device_id’s were intended for use with the UI editor and are not people friendly. I avoid them wherever I can, personally.

Why and how to avoid device_ids in automations and scripts.

Tanks @Didgeridrew. That gave me a good jump forward, but still not completely there. Right now the temperature is 18° but all relays came off. Yes! now it worked (at least at the current temperature) :grin: Ended up like this (apparently the web based yaml editor removed all comments):

alias: Simuler temperatur
description: Angiver aktuel temperatur i Sal for fjernvarmeunit
trigger:
  - platform: state
    entity_id:
      - sensor.simulator_vaerdi_2
condition: []
action:
  - service: >-
      light.turn_{{ 'on' if states("sensor.simulator_vaerdi_2") | int |
      bitwise_and(1) else 'off' }}
    target:
      entity_id: 855c33ae0a49cdb1c6dfd1e4667179f4
  - service: >-
      light.turn_{{ 'on' if states("sensor.simulator_vaerdi_2") | int |
      bitwise_and(2) else 'off' }}
    target:
      entity_id: 6679e11def8d7b93815029f919df1d8d
  - service: >-
      light.turn_{{ 'on' if states("sensor.simulator_vaerdi_2") | int |
      bitwise_and(4) else 'off' }}
    target:
      entity_id: 6a4fc0bc6a02877797e44ea893f61e1f
  - service: >-
      light.turn_{{ 'on' if states("sensor.simulator_vaerdi_2") | int |
      bitwise_and(8) else 'off' }}
    target:
      entity_id: cbbd0d2ee09146c56ee528a5bff59fa0
mode: restart

Thanks for your swift answer.

Thanks for your link, I’ll definitely have to make a updated version using entity_id.