Sensor with text, converting to numbers

Hi, I read/write data from/to a modbus.

I can publish a value via MQTT in integers 0-4 but when I read the value it is in text: "Normal, Hastighet 1, Hastighet 2, Hastighet 3, Hastighet 4)

I have a slider in lovelace that I try to update based on this information. I guess this should be easy but not for me :slight_smile:

Now I have the automation set as this, and this would work it the data it reads was in integers the same way as I write but now I guess I should convert it someway

alias: 'From Nibe: Set Fan Speed Slider'
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.nibe_fan_mode
condition: []
action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.nibe_fan_mode'') }}'
    target:
      entity_id: input_number.nibe_fan_mode
mode: single

What does this display in the template editor?

{{ states('sensor.nibe_fan_mode') }}

Hi, attached picture. It shows “Hastighet 2” now

So I’d like to do something like

If sensor.nibe.fan_mode = 'Hastighet 2' then
input.number.nibe_fan_mode:=2;

but not sure how to do that in HA as it is not my native language :slight_smile:

This should probably work if it always says "Hastighet " before the integer.

alias: 'From Nibe: Set Fan Speed Slider'
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.nibe_fan_mode
condition: []
action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.nibe_fan_mode'') | replace("Hastighet ", "") | int }}'
    target:
      entity_id: input_number.nibe_fan_mode
mode: single

Thanks for the reply Hellis81,
it did not seem to work straight away but feels like it should be close :slight_smile:

this is now my code:


alias: 'From Nibe: Set Fan Speed Slider'
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.nibe_fan_mode
condition: []
action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.nibe_fan_mode'') | replace("Hastighet ", "") | int }}'
    target:
      entity_id: input_number.nibe_fan_mode
mode: single

but as I set the fan to “Hastighet 3” from the unit it did call the automation but did not update the slider.

image

Or if it is always a single digit at the end, you could use Python’s slice:

action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.nibe_fan_mode'') [-1:] }}'
    target:
      entity_id: input_number.nibe_fan_mode

This [-1:] selects the last character of the string.

1 Like

Thank you Tom, I tried with this one also but I had no luck. I am a noob so there might be something silly I am missing.

This is the code now:

alias: 'From Nibe: Set Fan Speed Slider'
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.nibe_fan_mode
condition: []
action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.nibe_fan_mode'') [-1:] }}'
    target:
      entity_id: input_number.nibe_fan_mode
mode: single

In the developer tools it showed nicely

but despite running the actions it does not update the slider
image

(P.S. The Value can also be “Normal” then the slider should be at 0)

It might be something that the template editor does not support. Not sure Don’t use it.

It should work in YAML mode if it works in the template editor.

I might have a really silly misstake… The Helper slider is named NibeFan, not Nibe_fan_mode…Thanks for all the help!
Edit: Yes, it was just me having the wrong name on the helper… Thank you and sorry for my confusion!

In that case you probably need something like:

alias: 'From Nibe: Set Fan Speed Slider'
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.nibe_fan_mode
condition: []
action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.nibe_fan_mode'') | replace("Hastighet", "") | replace("Normal", "0") | trim | int }}'
    target:
      entity_id: input_number.nibefan  # make sure this is correct
mode: single
1 Like

Thank you this works perfectly :slight_smile: