Template sensor : how to use hex masking

Hi,

I am reading a mode-value from my heater, and need to translate it to a string.

This is the translation :

  • First I need to mask the value with 0x2F

  • Then I need to lookup the result from these possibilities :

                   modeB=MySensorValue & 0x2F;
      	if (modeB==8):
      		Modedescription='AUTO';
      	elif (modeB==36):
      		Modedescription='TEMP JOUR';
      	elif (modeB==34):
      		Modedescription='TEMP NUIT';
      	elif (modeB==4):
      		Modedescription='PERM JOUR';
      	elif (modeB==2):
      		Modedescription='PERM NUIT';
      	elif (modeB==1):
      		Modedescription='ANTIGEL';
    

How do I implement this in a template with a mapper?
I already have this

 - platform: template
    sensors:
     MyMode:
        friendly_name: "My Mode"
        value_template: >-
          {% set mapper =  {
              '8' : 'AUTO',
              '36' : 'TEMP JOUR',
              '34' : ''TEMP NUIT',
              '4' : 'PERM JOUR',
              '2' : 'PERM NUIT',
              '1': 'ANTIGEL'} %}
          {% set state =  states.sensor.MySensorValue.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

But I don’t know how to do the &2F masking in the template.

thx,
Thomas.

image

Reference: Numeric Functions and Filters

1 Like

Great! thx!

1 Like