Templates and multiple sensor

Hello, new user and tried to gather an understanding but need some guidance on template yaml

I can successfully create one sensor and it works but when I try to add another it only updates the last b.

Below is the code in the template yaml file.

sensor:
  name: scaled_amp_a
  unique_id: scaled_amp_a
  unit_of_measurement: "A"
  device_class: current
  state:
    "{{ (states('sensor.pwr_monitorct200_current_a','current') | float * 2.5 ) }}"


  name: scaled_amp_b
  unique_id: scaled_amp_b
  unit_of_measurement: "A"
  device_class: current
  state:
    "{{ (states('sensor.pwr_monitorct200_current_b','current') | float * 2.5 ) }}"

Template sensors need to be a list, not a dictionary:

sensor:
  - name: scaled_amp_a
    unique_id: scaled_amp_a
    unit_of_measurement: "A"
    device_class: current
    state: "{{ state_attr('sensor.pwr_monitorct200_current_a','current') * 2.5 }}"
    availability:  "{{ state_attr('sensor.pwr_monitorct200_current_a','current') | is_number }}"

  - name: scaled_amp_b
    unique_id: scaled_amp_b
    unit_of_measurement: "A"
    device_class: current
    state: "{{ state_attr('sensor.pwr_monitorct200_current_b','current') * 2.5 }}"
    availability:  "{{ state_attr('sensor.pwr_monitorct200_current_b','current') | is_number }}"

Use state_attr() to get an attribute value, not states().

Don’t mix up single and multi-line templates.

You should use an availability template or define defaults for your float() filters. You probably don’t even need these as only states are always strings. Attributes can be other types, like numbers.

Hi Tom, not sure i understand the reference to a dictionary?

I just updated my post. There were a lot of other issues.

For basic yaml (e.g. list vs dictionary) see: Thomas Lovén - YAML for Non-programmers

1 Like

I tried state_attr() in dev tools and it wouldn’t update the values but states did.
Just tried the change and thank you very much but it didn’t work, now I get unavailable on both.

Go to Developer Tools → States and find sensor.pwr_monitorct200_current_a1 take a screenshot of the entire row (including the right hand attribute column) and post it here. You can drag and drop the image into the post.

This is gibberish:

states('sensor.pwr_monitorct200_current_a','current') 

This is accessing the state value of the sensor (2nd column in your picture):

states('sensor.pwr_monitorct200_current_a') 

This is accessing the current attribute of the sensor (3rd column in your picture):

state_attr('sensor.pwr_monitorct200_current_a','current') 

Your sensor does not have a current attribute, so:

sensor:
  - name: Scaled Amp A
    unique_id: scaled_amp_a
    unit_of_measurement: "A"
    device_class: current
    state: "{{ states('sensor.pwr_monitorct200_current_a') | float * 2.5 }}"
    availability:  "{{ has_value('sensor.pwr_monitorct200_current_a') }}"

  - name: Scaled Amp B
    unique_id: scaled_amp_b
    unit_of_measurement: "A"
    device_class: current
    state: "{{ state('sensor.pwr_monitorct200_current_b') | float * 2.5 }}"
    availability:  "{{ has_value('sensor.pwr_monitorct200_current_b') }}"

A is working but B is not

Share the YAML you are using.

Fixed! I must’ve pasted it wrong. WOW thank you ever so much Tom!